<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/17
 * Time: 14:26
 */

namespace app\admin\controller;

use think\Db;
use think\Validate;

/**
 * Class Shops
 * @package app\admin\controller 商铺管理
 */
class Shops extends Base
{
    public function index()
    {
        $num = Db::name('user')->count();
        $pageObject = new \Page($num);
        $list = Db::name('user')
            ->select();

        $map = array(
            'page' => $pageObject->showPages(),
            'list' => $list,
            'num' => $num,
        );
        return view('index', $map);
    }

    public function edit()
    {

        if ($_POST) {

            $data = $_POST;

            if (Db::name('goods_group')->where(array('id' => $data['pid'], 'pid' => 0))->find()) {
                $this->error('请选择子分类 ！');
            }

            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            if (Db::name('user')->update($data)) {
                $this->success('修改成功！');
            } else {
                $this->error('修改失败！');
            }

        } else {
            $list = Db::name('user')
                ->where(array('id' => input('id')))
                ->find();

            $group_list = Db::name('goods_group')->where(array('status' => 1))->select();

            $map = array(
                'list' => $list,
                'group_list' => menu_left($group_list)
            );

            return view('edit', $map);
        }
    }

    public function add()
    {

    }

    public function del()
    {
        $id = input('id');

        if (!$id) {

            $this->error('错误操作！');

        }

        if (Db::name('user')->delete(array('id' => $id))) {

            $this->success('删除用户成功！');

        } else {

            $this->error('删除用户失败！');

        }

    }

}